package qc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;

import ui.MainFrame;

import beans.FileBean;
import beans.UserBean;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;

public class DeleteFileBCO implements ControlObject {

	@Override
	public Object handleIt(ArrayList<Object> arg0) {

		HashMap params = (HashMap) arg0.get(arg0.size()-1);
		String fileId = (String) params.get("fileId");
		
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement delete = null;
		boolean worked = false;
		ResultSet results = null;
		String filename = "";
		try {
			delete = con.prepareStatement("SELECT FileName FROM File WHERE FileNumber=? AND FileActiveFlag=1");
			delete.setString(1, fileId);
			results = delete.executeQuery();
			while (results.next()) {
				filename = results.getString(1);
			}
			
			delete = con.prepareStatement("UPDATE File SET FileActiveFlag=0 WHERE FileNumber=? AND FileActiveFlag=1");
			delete.setString(1, fileId);
			delete.execute();
			worked = true;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
		params.put("success", worked);
		ServerConnectionHandler handler = (ServerConnectionHandler) arg0.get(0);
		UserBean curUser = handler.getUser();
		params.put("serverNotification", curUser.getUsername() + " has deleted " + filename);
		return params;
		
	}

}
